﻿using System;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace TackMessenger
{
    public partial class Messenger : Form
    {

        Encryption enc;

        public Messenger()
        {
            InitializeComponent();
            
            this.KeyPreview = true;
            getPublicIP();
            enc = new Encryption();
            //Client = new Client(Props.address, Props.port);
            //Client.listen();
            }

        private void getPublicIP()
        {
            try
            {
                Props.setMyIpAddress(getMyIp());
                Props.Client = true;
                extIP.ForeColor = System.Drawing.Color.Black;
                extIP.Text = "Your IP: "+ Props.getMyIp().ToString();
                Props.localAddress = IPAddress.Parse(Props.getMyIp().ToString());
            }
            catch (Exception)
            {
                extIP.ForeColor = System.Drawing.Color.Red;
                extIP.Text = "Your IP: No Client.";
                Props.Client = false;
            }
        }


       

        private string getMyIp()
        {
            String ipAddress = "";
            String result = "";
            PartyPlace.AppendText("--- Attempting to acquire public IP Address... ");
            try
            {
                pBar.Increment(10);
                WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
                request.Method = "POST";
                request.Credentials = CredentialCache.DefaultCredentials;
                pBar.Increment(10);
                Stream dataStream = request.GetRequestStream();
                WebResponse response = request.GetResponse();
                pBar.Increment(10);
                PartyPlace.AppendText(((HttpWebResponse)response).StatusDescription + "\n");
                dataStream = response.GetResponseStream();
                pBar.Increment(10);
                StreamReader reader = new StreamReader(dataStream);
                ipAddress = reader.ReadToEnd();
                pBar.Increment(10);
                Char[] address = ipAddress.ToCharArray();
                Char ch;
                pBar.Increment(10);
                for (int i = 0; i < ipAddress.Length; ++i)
                {
                    ch = address[i];
                    if (ch == '.' || Char.IsNumber(ch))
                    {
                        result += address[i];
                    }

                }
                pBar.Increment(10);
                response.Close();
                reader.Close();
                if (result.Length == 0)
                {
                    result = "unkown";
                } pBar.Increment(10);
            }
            catch (WebException)
            {
                PartyPlace.AppendText("Failed\nPlease make sure you are connected to the internet.\n");
 
            
            
            }

            pBar.Increment(20);

            return result;
        }


        private void Send_Click(object sender, EventArgs e)
        {
            if (TextArea.Text != "")
            {
                PartyPlace.AppendText("     " + Props.getUsername() + " says: \n" + TextArea.Text + "\n\n");
             //   PartyPlace.AppendText("Decoded: " + enc.decode(enc.encode(TextArea.Text)) + "\n");
                TextArea.Text = "";
                TextArea.Focus();

            }
        }

        private void setNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NameGetter name = new NameGetter();
            name.Show();
        }

        

        private void TextArea_TextChanged(object sender, EventArgs e)
        {
            this.KeyDown += new KeyEventHandler(Form1_KeyDown);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter && !e.Shift)
            {
                e.SuppressKeyPress = true;
                if (TextArea.Text != "")
                {
                    PartyPlace.AppendText("    " + Props.getUsername() + " says: \n" + TextArea.Text + "\n\n");
                    TextArea.Text = "";
                }
            }
        }

        private void connectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConnectDialog connect = new ConnectDialog();
            connect.Show();
        }
      

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void extIP_Click(object sender, EventArgs e)
        {
            if (!Props.Client)
            {
                getPublicIP();
            }
        }





       
       

    }
}
